/* $RCSFile: CRMImageBookImageItem.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ImageSettings/UI/WebControls/CRMImageBookImageItem.cs $
 * $log$
 * Revision 7 2010/08/09 09:44:02  christian.surieux
 *   Added Log to cs sources files
 */

using System;
using System.Xml;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.Settings;
using AddonNice.Diagnostics;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Core;


using AddonNice.Settings.CRMImage;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.CRMImage.UI.WebControls
{
    /// <summary>
    /// CRMImageBookImageItem manages an individual image inside one ImageBook
    /// Inherits from CrmImageItem just to redefine the path to view items
    /// </summary>
    public class CRMImageBookImageItem : CRMImageItem
    {

        RepeatDirection _RepeatDirection = RepeatDirection.Horizontal;
        public RepeatDirection RepeatDirection    
        {
            get
            {
                return _RepeatDirection;
            }
            set
            {
                _RepeatDirection = value;
            }
        }
	    int _RepeatColumns = 1;
	    public int RepeatColumns 
        {
            get
            {
                return _RepeatColumns;
            }
            set
            {
                _RepeatColumns = value;
            }
        }
        string _ThumbnailLayout = string.Empty;
        public string ThumbnailLayout
        {
            get
            {
                return _ThumbnailLayout;
            }
            set
            {
                _ThumbnailLayout = value;
            }
        }

        /// <summary>
        /// EntityName read from DB when browsing records not under CRM
        /// </summary>
        public string DBEntityName
        {
            get
            {
                return GetMetadata(ImStr.MetaDBEntityName);
            }
        }
        
        /// <summary>
        /// value for the 'display name' attribute specified in the settings for ImageBook module
        /// </summary>
        public string AttachedName
        {
            get
            {
                return GetMetadata(ImStr.AttachedName);
            }
        }

        public override string StdArgString
        {
            get
            {
				string str  =   string.Format("ImageDBID={0}&ItemCrmID={1}&typename={2}&id={3}&mid={4}&DBEntityName={5}&PageNum={6}&ParentPageNum={7}",
                    new object[]{GetMetadata(ImStr.ImageDBID),
                        GetMetadata(ImStr.ItemCrmID),
                        GetMetadata(ImStr.RequestEntityName),
                        GetMetadata(ImStr.EntityID),
                        GetMetadata(ImStr.ModuleID),
                        DBEntityName,
                        GetMetadata(ImStr.PageNum),
                        GetMetadata(ImStr.ParentPageNum)});
                    if ( CrmImageTrace.Sw.TraceVerbose )
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CRMImageBookImageItem StdArgString GET: {0}", str),CrmImageTrace.Sw.Info);
                return str;
            }
        }

        public override string imageEdit
		{
			get
			{
				return HttpUrlBuilder.BuildUrl(Config.ModulesPath+"/CRMImageBook/CRMImageBookEdit.aspx",StdArgString);
			}
		}
		public override string imageView
		{
			get
			{
				return HttpUrlBuilder.BuildUrl(Config.ModulesPath+"/CRMImage/CRMImageView.aspx",StdArgString);
			}
		}

        /// <summary>
		/// Attempt to cache the layout module name
		/// </summary>
		private string _layoutThumbnailModule	=	string.Empty;
		string layoutThumbnailModule
		{
			get
			{
				if (string.IsNullOrEmpty(_layoutThumbnailModule))
				{
					_layoutThumbnailModule = Config.ModulesPath + "/CRMImageBook/Layout/Thumbnails/" + ThumbnailLayout;
					if ( ModuleTraceSwitch.Sw.TraceVerbose )
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CRMImageBookImageItem layoutModule: {0}", _layoutThumbnailModule),ModuleTraceSwitch.Sw.Info);
				}
				return _layoutThumbnailModule;
			}
		}

        private void CRMImageBookImageItem_Load(object sender, System.EventArgs e)
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]CRMImageBookImageItem CRMImageBookImageItem_Load BEG.",ModuleTraceSwitch.Sw.Info);
            DataBind();
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]CRMImageBookImageItem CRMImageBookImageItem_Load END.",ModuleTraceSwitch.Sw.Info);
        }
        

		#region Std Overrides

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }
		
        private void InitializeComponent()
        {
            Load        +=  new EventHandler(CRMImageBookImageItem_Load);
		}

        #endregion
    }
}